package config

import (
	"chatgpt/helper"
	"github.com/spf13/viper"
)

type ChatGPTConf struct {
	// gpt apikey
	ApiKey  string `json:"api_key"`
	Davinci GPTDavinciConf
	Chat    GPTChatConf
	Image   GPTImageConf
}

type GPTDavinciConf struct {
	Port int `json:"port"`
	//
	BotDesc string `json:"bot_desc"`
	// GPT请求最大字符数
	MaxTokens int `json:"max_tokens"`
	// GPT模型
	Model string `json:"model"`
	// 热度
	Temperature      float64 `json:"temperature"`
	TopP             float32 `json:"top_p"`
	PresencePenalty  float32 `json:"presence_penalty"`
	FrequencyPenalty float32 `json:"frequency_penalty"`
	Open             bool    `toml:"open"`
}

func (self *GPTDavinciConf) Load(onf *viper.Viper) {
	self.BotDesc = onf.GetString("bot_desc")
	self.MaxTokens = onf.GetInt("max_tokens")
	self.Model = onf.GetString("model")
	self.Temperature = onf.GetFloat64("temperature")
	self.TopP = float32(onf.GetFloat64("top_p"))
	self.PresencePenalty = float32(onf.GetFloat64("presence_penalty"))
	self.Open = onf.GetBool("open")
}

type GPTChatConf struct {
	Port int `json:"port"`
	//
	BotDesc string `json:"bot_desc"`
	// GPT请求最大字符数
	MaxTokens int `json:"max_tokens"`
	// GPT模型
	Model string `json:"model"`
	// 热度
	Temperature      float64 `json:"temperature"`
	TopP             float32 `json:"top_p"`
	PresencePenalty  float32 `json:"presence_penalty"`
	FrequencyPenalty float32 `json:"frequency_penalty"`
	Open             bool    `toml:"open"`
}

type GPTImageConf struct {
	Size string
	N    int
}

func (self *GPTImageConf) Load(onf *viper.Viper) {
	self.Size = onf.GetString("size")
	self.N = onf.GetInt("n")
}

func (self *GPTChatConf) Load(onf *viper.Viper) {
	self.BotDesc = onf.GetString("bot_desc")
	self.MaxTokens = onf.GetInt("max_tokens")
	self.Model = onf.GetString("model")
	self.Temperature = onf.GetFloat64("temperature")
	self.TopP = float32(onf.GetFloat64("top_p"))
	self.PresencePenalty = float32(onf.GetFloat64("presence_penalty"))
	self.Open = onf.GetBool("open")
}

func (self *ChatGPTConf) Load() {
	cnf := helper.Config().Sub("ChatGPT")
	self.ApiKey = cnf.GetString("api_key")
	self.Davinci.Load(cnf.Sub("davinci"))
	self.Chat.Load(cnf.Sub("chat"))
	self.Image.Load(cnf.Sub("image"))
}
